<template>
  <div>
    <div
      class="capital-ware-house-information"
      v-if="!appStore.isPhone">
      <!--资金仓库信息-->
      <el-card
        class="search_card"
        ref="searchCardRef">
        <el-form
          :model="formData"
          class="search_form">
          <el-row>
            <el-form-item label="主体公司简称">
              <el-select
                v-model="formData.baseCompanyId"
                placeholder="主体公司简称"
                filterable
                style="width: 150px"
                clearable>
                <el-option
                  v-for="item in mainOrgSimplyOptions"
                  :key="item.id"
                  :label="item.simple_name"
                  :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="资金方简称">
              <el-select
                v-model="formData.baseCapitalPartyId"
                placeholder="资金方简称"
                style="width: 150px"
                filterable
                clearable>
                <el-option
                  v-for="item in baseCapitalPartySimpleNameOptions"
                  :key="item.id"
                  :label="item.simple_name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="仓库简称">
              <el-select
                v-model="formData.baseWarehouseId"
                placeholder="仓库简称"
                style="width: 150px"
                filterable
                clearable>
                <el-option
                  v-for="item in warehouseAbbreviationList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-button
              type="primary"
              @click="getList(formData)"
              class="query_button">
              <template #icon>
                <el-icon>
                  <search />
                </el-icon>
              </template>
              {{ config.button.text.query }}
            </el-button>
            <el-button @click="reset">
              <template #icon>
                <el-icon>
                  <Refresh />
                </el-icon>
              </template>
              {{ config.button.text.reset }}
            </el-button>
          </el-row>
        </el-form>
        <el-button
          type="info"
          v-permission="validateButtonPermission(route, 0)"
          @click="addEvent">
          <template #icon>
            <el-icon>
              <CirclePlus />
            </el-icon>
          </template>
          {{ config.button.text.add }}
        </el-button>
      </el-card>
      <el-card class="table_card">
        <Table
          :data="tableData"
          v-loading="tableLoading"
          :height="height">
          <el-table-column
            prop="baseCompanySimpleName"
            show-overflow-tooltip
            min-width="120"
            :resizable="false"
            label="主体公司" />
          <el-table-column
            prop="baseCapitalPartySimpleName"
            show-overflow-tooltip
            min-width="120"
            :resizable="false"
            label="资金方简称" />
          <el-table-column
            prop="baseWarehouseName"
            :resizable="false"
            show-overflow-tooltip
            min-width="120"
            label="仓库名称" />
          <el-table-column
            prop="name"
            :resizable="false"
            min-width="120"
            show-overflow-tooltip
            label="资金仓库名称" />
          <el-table-column
            prop="dailyChemicalRate"
            show-overflow-tooltip
            min-width="80"
            :resizable="false"
            label="日化率">
            <template #default="{row}">
              {{ row.dailyChemicalRate }}<span v-if="row.dailyChemicalRate">%</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="companyFirstPayPercent"
            min-width="140"
            show-overflow-tooltip
            :resizable="false"
            label="我司预付款百分比">
            <template #default="{row}">
              {{ row.companyFirstPayPercent }}<span v-if="row.companyFirstPayPercent">%</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="capitalFirstPayPercent"
            show-overflow-tooltip
            min-width="140"
            :resizable="false"
            label="资方预付款百分比">
            <template #default="{row}">
              {{ row.capitalFirstPayPercent }}<span v-if="row.capitalFirstPayPercent">%</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="capitalEndPayPercent"
            show-overflow-tooltip
            :resizable="false"
            min-width="140"
            label="资方尾款百分比">
            <template #default="{row}">
              {{ row.capitalEndPayPercent }}<span v-if="row.capitalEndPayPercent">%</span>
            </template>
          </el-table-column>
          <el-table-column
            label="启用状态 "
            min-width="100"
            :resizable="false">
            <template #default="scope">
              <el-tag :type="scope.row.isEnable ? 'primary' : 'danger'">
                {{ scope.row.isEnable ? config.button.text.enable
                  : config.button.text.stop }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="remark"
            :resizable="false"
            min-width="100"
            show-overflow-tooltip
            label="备注" />
          <el-table-column
            label="操作"
            :resizable="false"
            fixed="right"
            width="180">
            <template #default="scope">
              <el-button
                type="warning"
                v-permission="validateButtonPermission(route, 1)"
                @click="editEvent(scope.row)">
                {{ config.button.text.edit }}
              </el-button>
              <el-button
                type="success"
                @click="viewEvent(scope.row)">
                {{ config.button.text.view }}
              </el-button>
            </template>
          </el-table-column>
        </Table>
        <Pagination
          :pageNum="pageNum"
          :pageSize="pageSize"
          :total="total"
          @handle-size-change="(val) => pageSize = val"
          @handle-current-change="(val) => pageNum = val" />
      </el-card>
      <!-- 查看 -->
      <View
        ref="viewRef"
        :rowData="rowData" />
      <!-- 新增修改 -->
      <Edit
        ref="editRef"
        :rowData="rowData"
        @save="save" />
    </div>
    <!-- 手机端页面 -->
    <h5Index v-else></h5Index>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import Pagination from "@/components/Pagination.vue"
import Table from "@/components/Table.vue"
import ApiBaseCapitalWarehouse from "@/api/modules/capital-ware-house"
import h5Index from "./h5Index.vue";
import { useTable } from "@/hooks/useTable"
import View from "./component/View.vue"
import Edit from "./component/Edit.vue"
import message from "@/common/element-plus/message";
import config from "@/common/element-plus/config";
import app from "@/common/config/app";
import { validateButtonPermission } from "@/common/utils/validate";
import { useRoute } from "vue-router"
import { useAppStore } from "@/stores/app"
import { storeToRefs } from "pinia";

const route = useRoute()
// 查询表单
const formData = ref({})
// 查看组件实例
const viewRef = ref()
// 新增修改组件实例
const editRef = ref()
// 表格每行数据
const rowData = ref({})
// 表格高度(固定表头)
const height = ref(0)
// 查询卡片实例
const searchCardRef = ref()
// 新增实例
// const addRef = ref()
//资方简称列表
const baseCapitalPartySimpleNameOptions = ref([]);
//仓库简称列表
const warehouseAbbreviationList = ref([]);
//主体公司简称列表
const mainOrgSimplyOptions = ref([]);

const { tableMethods, tableState, pagination } = useTable({
  fetchDataApi: async (data) => {
    const { pageSize, pageNum } = pagination
    const res = await ApiBaseCapitalWarehouse.selectPage(data, {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
    })
    return {
      list: res.data.records,
      total: res.data.total
    }
  }
})

//获取主体公司简称列表
const getMainOrgList = async () => {
  const res = await ApiBaseCapitalWarehouse.getSimpleNameList("/baseCompany/");
  if (res?.code == 200 && Array.isArray(res?.data) && res?.data.length > 0) {
    mainOrgSimplyOptions.value = res.data;
  }
}
getMainOrgList()

//获取资金方简称列表
const getSelectShortForManagement = async () => {
  const res = await ApiBaseCapitalWarehouse.getSimpleNameList("/baseCapitalParty/");
  if (res?.code==200&&Array.isArray(res?.data)&&res?.data.length>0){
    baseCapitalPartySimpleNameOptions.value=res.data;
  }
}

//获取仓库简称列表
const getSelectWarehouseAbbreviation = async () => {
  const res = await ApiBaseCapitalWarehouse.getSimpleNameList("/baseWarehouse/")
  if (res?.code==200&&Array.isArray(res?.data)&&res?.data.length>0){
    warehouseAbbreviationList.value=res.data;
  }
}

const { getList } = tableMethods
const { pageSize, pageNum } = pagination
const { total, tableLoading, tableData } = tableState

// 重置
const reset = () => {
  formData.value = {}
  getList()
}

// 打开查看对话框
const viewEvent = (row) => {
  viewRef.value.visibility = true
  rowData.value = row
}

// 打开新增对话框
const addEvent = () => {
  editRef.value.visibility = true
  rowData.value = {}
}

// 打开修改对话框
const editEvent = (row) => {
  editRef.value.visibility = true
  rowData.value = Object.assign({}, row)
}

// 保存后回调
const save = async () => {
  editRef.value.visibility = false
  message.success()
  await getList()
}

// 监听查询卡片大小变化
const resizeObserver = new ResizeObserver(entries => {
  // if (!addRef.value) return
  for (const entry of entries) {
    if (contentFullScreen.value) {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - entry.contentRect.height) + "px"
    } else {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - app.headerHeight - entry.contentRect.height) + "px"
    }
  }
})

const appStore = useAppStore()

// 保持响应式
const { contentFullScreen } = storeToRefs(appStore)

onMounted(() => {
  if (appStore.isPhone) return
  resizeObserver.observe(searchCardRef.value.$el)
  getSelectShortForManagement();
  getSelectWarehouseAbbreviation();
})
</script>

<style lang="scss" scoped>
.capital-ware-house-information {
  .search_card {
    margin-bottom: 4px;

    :deep(.el-card__body) {
      padding: 10px;
      display: flex;
      justify-content: space-between;
    }

    .el-button {
      margin: 5px 10px 5px 0;
    }

    .query_button {
      margin-left: 100px;
    }

    .el-form-item {
      margin-right: 10px;
      padding: 5px 0;
    }
  }

  .table_card {
    :deep(.el-card__body) {
      padding: 0;
    }
  }
}
</style>
